package demoMod.stsconsole.helpers;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import demoMod.stsconsole.ConsoleEntry;
import demoMod.stsconsole.interfaces.ReloadStringsSubscriber;
import demoMod.stsconsole.settings.ConsoleSettings;

import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings({"ConstantConditions"})
public class LocalizedStrings {

    private static final Map<String, Map<String, List<String>>> strings = new HashMap<>();

    public static void initialize() {
        strings.clear();
        Gson gson = new Gson();
        TypeToken<Map<String, List<String>>> token = new TypeToken<Map<String, List<String>>>(){};

        Map<String, List<String>> uiStrings;
        uiStrings = gson.fromJson(new InputStreamReader(LocalizedStrings.class.getClassLoader().getResourceAsStream("localization/" + ConsoleSettings.language + "/ui.json")), token.getType());
        strings.put("ui", uiStrings);

        Map<String, List<String>> commandStrings;
        commandStrings = gson.fromJson(new InputStreamReader(LocalizedStrings.class.getClassLoader().getResourceAsStream("localization/" + ConsoleSettings.language + "/command.json")), token.getType());
        strings.put("command", commandStrings);

        Map<String, List<String>> powerStrings;
        powerStrings = gson.fromJson(new InputStreamReader(LocalizedStrings.class.getClassLoader().getResourceAsStream("localization/" + ConsoleSettings.language + "/powers.json")), token.getType());
        strings.put("powers", powerStrings);

        Map<String, List<String>> relicStrings;
        relicStrings = gson.fromJson(new InputStreamReader(LocalizedStrings.class.getClassLoader().getResourceAsStream("localization/" + ConsoleSettings.language + "/relics.json")), token.getType());
        strings.put("relics", relicStrings);

        Map<String, List<String>> cardStrings;
        cardStrings = gson.fromJson(new InputStreamReader(LocalizedStrings.class.getClassLoader().getResourceAsStream("localization/" + ConsoleSettings.language + "/cards.json")), token.getType());
        strings.put("cards", cardStrings);

        Map<String, List<String>> characterStrings;
        characterStrings = gson.fromJson(new InputStreamReader(LocalizedStrings.class.getClassLoader().getResourceAsStream("localization/" + ConsoleSettings.language + "/characters.json")), token.getType());
        strings.put("characters", characterStrings);

        Map<String, List<String>> potionStrings;
        potionStrings = gson.fromJson(new InputStreamReader(LocalizedStrings.class.getClassLoader().getResourceAsStream("localization/" + ConsoleSettings.language + "/potions.json")), token.getType());
        strings.put("potions", potionStrings);

        Map<String, List<String>> monsterStrings;
        monsterStrings = gson.fromJson(new InputStreamReader(LocalizedStrings.class.getClassLoader().getResourceAsStream("localization/" + ConsoleSettings.language + "/monsters.json")), token.getType());
        strings.put("monsters", monsterStrings);

        Map<String, List<String>> rewardStrings;
        rewardStrings = gson.fromJson(new InputStreamReader(LocalizedStrings.class.getClassLoader().getResourceAsStream("localization/" + ConsoleSettings.language + "/rewards.json")), token.getType());
        strings.put("rewards", rewardStrings);

        Map<String, List<String>> eventStrings;
        eventStrings = gson.fromJson(new InputStreamReader(LocalizedStrings.class.getClassLoader().getResourceAsStream("localization/" + ConsoleSettings.language + "/events.json")), token.getType());
        strings.put("events", eventStrings);

        ConsoleEntry.triggerSubscribers(ReloadStringsSubscriber.class, "onReloadStrings");
        ConsoleEntry.config.setString("language", ConsoleSettings.language);
        ConsoleEntry.config.save();
    }

    public static List<String> getUIStrings(String id) {
        return strings.get("ui").get(id);
    }

    public static List<String> getCommandStrings(String id) {
        return strings.get("command").get(id);
    }

    public static List<String> getPowerStrings(String id) {
        return strings.get("powers").get(id);
    }

    public static List<String> getRelicStrings(String id) {
        return strings.get("relics").get(id);
    }

    public static List<String> getCardStrings(String id) {
        return strings.get("cards").get(id);
    }

    public static List<String> getCharacterStrings(String id) {
        return strings.get("characters").get(id);
    }

    public static List<String> getPotionStrings(String id) {
        return strings.get("potions").get(id);
    }

    public static List<String> getMonsterStrings(String id) {
        return strings.get("monsters").get(id);
    }

    public static List<String> getRewardStrings(String id) {
        return strings.get("rewards").get(id);
    }

    public static List<String> getEventStrings(String id) {
        return strings.get("events").get(id);
    }
}
